package Com.Isman.Booking;

import java.util.ArrayList;
import java.util.List;

import Com.Isman.FinalVariable;
import Com.Isman.Adapter.HotelSearchAdapter;
import Com.Isman.WebService.NetworkCallSearchListener;
import Com.Isman.WebService.ServiceConnect;
import Com.Isman.WebService.ComplexType.Hotel;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

public class PageSearchHotel extends Activity {
	
	class MenuAction implements NetworkCallSearchListener{
    	public MenuAction(MenuItem item){
            switch (item.getItemId()) {
            case R.id.itemHome:
            	Toast.makeText(PageSearchHotel.this, "About", Toast.LENGTH_SHORT).show();
                break;
            case R.id.itemRefresh:
            	Toast.makeText(PageSearchHotel.this, "Exit", Toast.LENGTH_SHORT).show();
                break;
            case R.id.itemLocation:
    			list.clear();
    			adapter.notifyDataSetChanged();
    			
    			String str = edtInput.getText().toString();
    			callByLocalTask = new NetworkCallSearchByLocationTask(this);
    			callByLocalTask.execute(new String[]{str,str,list.size()+"","2"});
                break;
            case R.id.itemOrder:
            	Intent intent = new Intent(PageSearchHotel.this,PageOrderList.class);
            	startActivity(intent);
                break;
            case R.id.itemOption:
            	Intent pre = new Intent(PageSearchHotel.this,HotelPreference.class);
            	startActivity(pre);
                break;
            case R.id.itemFeedBack:
            	Toast.makeText(PageSearchHotel.this, "Refresh", Toast.LENGTH_SHORT).show();
                break;
            }
    	}

		@Override
		public void onNetworkCallCancel() {
			// TODO Auto-generated method stub
		}
		@Override
		public void onNetworkCallComplete(List<Hotel> lists) {
			list.addAll(lists);
			adapter.notifyDataSetChanged();
		}
    }
	class NetworkCallSearchByLocationTask extends AsyncTask<String, Void, List<Hotel>>{
		private NetworkCallSearchListener listener = null;
		public NetworkCallSearchByLocationTask(NetworkCallSearchListener listener) {
			this.listener = listener;
		}
		@Override
		protected List<Hotel> doInBackground(String... params) {
		   Log.v("Isman", "doInBackground(String... params)");
		   ServiceConnect sc = new ServiceConnect();
		   return sc.callGetHotelListByLocation(params[0],params[1], Integer.parseInt(params[2]), Integer.parseInt(params[3]));
		}		
		@Override
		protected void onPostExecute(List<Hotel> lists) {
		   	if(footerViewMore != null)
		   		searchList.removeFooterView(footerViewMore);
		   	
		   	footerViewMore = ((LayoutInflater)PageSearchHotel.this.getSystemService
				(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.itemmorelist, null);
			Button btnMore =  (Button)footerViewMore.findViewById(R.id.btnMore);
			btnMore.setOnClickListener(new SearchAction());
		   		
			if(lists.size() < 2)
				btnMore.setEnabled(false);
			
			searchList.addFooterView(footerViewMore);
			listener.onNetworkCallComplete(lists);
			Log.v("Isman", "onPostExecute(List<Hotel> list)");
		}
		@Override
		protected void onPreExecute() {
		    super.onPreExecute(); 
		   	if(footerViewMore != null)
		   		searchList.removeFooterView(footerViewMore);
		   	
	        footerViewMore = ((LayoutInflater)PageSearchHotel.this.getSystemService
					(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.itemwaitlist, null);
	        searchList.addFooterView(footerViewMore);
		}
	}
	class NetworkCallSearchTask extends AsyncTask<String, Void, List<Hotel>>{
		private NetworkCallSearchListener listener = null;
		
		public NetworkCallSearchTask(NetworkCallSearchListener listener) {
			this.listener = listener;
		}
		@Override
		protected List<Hotel> doInBackground(String... params) {
		   Log.v("Isman", "doInBackground(String... params)");
		   ServiceConnect sc = new ServiceConnect();
		   return sc.callGetHotelList(params[0], Integer.parseInt(params[1]), Integer.parseInt(params[2]));
		}		
		@Override
		protected void onPostExecute(List<Hotel> lists) {
		   	if(footerViewMore != null)
		   		searchList.removeFooterView(footerViewMore);
		   	
		   	footerViewMore = ((LayoutInflater)PageSearchHotel.this.getSystemService
				(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.itemmorelist, null);
			Button btnMore =  (Button)footerViewMore.findViewById(R.id.btnMore);
			btnMore.setOnClickListener(new SearchAction());
		   		
			if(lists.size() < 2)
				btnMore.setEnabled(false);
			
			searchList.addFooterView(footerViewMore);
			listener.onNetworkCallComplete(lists);
			Log.v("Isman", "onPostExecute(List<Hotel> list)");
		}  
		@Override
		protected void onPreExecute() {
		    super.onPreExecute(); 
		   	if(footerViewMore != null)
		   		searchList.removeFooterView(footerViewMore);
		   	
	        footerViewMore = ((LayoutInflater)PageSearchHotel.this.getSystemService
					(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.itemwaitlist, null);
	        searchList.addFooterView(footerViewMore);
		}
	}
	class SearchAction implements OnClickListener, OnEditorActionListener, NetworkCallSearchListener{
		
    	@Override
		public void onClick(View v) {
    		String str = edtInput.getText().toString();
    		callTask = new NetworkCallSearchTask(SearchAction.this);
    		callTask.execute(new String[]{str,(list.size())+"","2"});
        }
		@Override
		public boolean onEditorAction(TextView tv, int arg1, KeyEvent key) {
			list.clear();
			adapter.notifyDataSetChanged();
			
			String str = edtInput.getText().toString();
			callTask = new NetworkCallSearchTask(this);
	 		callTask.execute(new String[]{str,list.size()+"","2"});
			return false;
		}
		@Override
		public void onNetworkCallCancel() {
			// TODO Auto-generated method stub
		}
		@Override
		public void onNetworkCallComplete(List<Hotel> lists) {
			list.addAll(lists);
			adapter.notifyDataSetChanged();
		}
	}
	
	private HotelSearchAdapter adapter;
	
	private ImageButton btnCalendar;
	private ImageButton btnMapView;
	private ImageButton btnPeople;
	private NetworkCallSearchByLocationTask callByLocalTask;
	private NetworkCallSearchTask callTask;

	

	private EditText edtInput;	
    private View footerViewMore = null;
    private List<Hotel> list = new ArrayList<Hotel>();
    private ListView searchList;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pagesearchhotel);
        
        SharedPreferences setting = getSharedPreferences(FinalVariable.APP_PREFERENCES, Context.MODE_WORLD_READABLE);
        double doLa  = setting.getFloat(FinalVariable.APP_LOCATION_LATITUBE, 0.0f);
        double doLo  = setting.getFloat(FinalVariable.APP_LOCATION_LONGITUBE, 0.0f);
        Log.v("Isman", doLa+"");
        Log.v("Isman", doLo+"");
        
        
        searchList 	= (ListView)findViewById(R.id.searchList);
        btnPeople 	= (ImageButton)findViewById(R.id.btnSearchPeople);
        btnCalendar = (ImageButton)findViewById(R.id.btnSearchCalendar);
        btnMapView 	= (ImageButton)findViewById(R.id.btnSearchMap);
        edtInput 	= (EditText)findViewById(R.id.edtSearchInput);
        edtInput.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        

        btnCalendar.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(0);
			}
		});
		btnPeople.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(1);
			}
		});
		btnMapView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v){
				Intent intent = new Intent(v.getContext(),PageMap.class);
				v.getContext().startActivity(intent);
			}
		});
		edtInput.setOnEditorActionListener(new SearchAction());
		
		
		onLoadDefult();
	}
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.searchmenu, menu);
	    return true;
    }
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
    	new MenuAction(item);
        return true;
    }
    @Override
    protected Dialog onCreateDialog(int id) {
    	LayoutInflater li = null;
        switch (id) {
            case 0:
                li = LayoutInflater.from(this);
                View dialogcalendar = li.inflate(R.layout.dialogcalendar, null);
                
                AlertDialog.Builder dialogcalendarlBuilder = new AlertDialog.Builder(this);
                dialogcalendarlBuilder.setTitle("Calendar");
                dialogcalendarlBuilder.setView(dialogcalendar);
                AlertDialog dialog = dialogcalendarlBuilder.create();
       
                
                dialog.setButton("Set", new DialogInterface.OnClickListener() {		
					@Override
					public void onClick(DialogInterface dialog, int which) {
						edtInput.setText("ads");
					}
				});
                dialog.setButton2("Close",new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
					}
				});
                
                return dialog;
            case 1:
                li = LayoutInflater.from(this);
                View dialogpeople = li.inflate(R.layout.dialogpeople, null);
                

                
                 
                
                AlertDialog.Builder dialogpeopleBuilder = new AlertDialog.Builder(this);
                dialogpeopleBuilder.setTitle("Edit Category");
                dialogpeopleBuilder.setView(dialogpeople);
                AlertDialog dialog1 = dialogpeopleBuilder.create();
                
                dialog1.setButton("Set", new DialogInterface.OnClickListener() {		
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
					}
				});
                dialog1.setButton2("Close",new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
					}
				});
            return dialog1;
        }
        return null;
    }
	protected void onLoadDefult(){	
        footerViewMore = ((LayoutInflater)PageSearchHotel.this.getSystemService
				(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.itemresultlist, null);
		searchList.addFooterView(footerViewMore);
	
		adapter = new HotelSearchAdapter(this,list);
        searchList.setAdapter(adapter);
    }
}